package super

import (
	"Project/global"
	"Project/model/common/response"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type AreaApi struct{}

// 获取区县列表
func (area *AreaApi) GetAreaList(c *gin.Context) {
	// 查询区县
	areaList, err := areaService.GetAreaList()
	if err != nil {
		response.Fail(c)
		return
	}
	// 返回
	response.OkWithData(areaList, c)
}

// 获取区县ID列表
func (area *AreaApi) GetAreaIdList(c *gin.Context) {
	// 查询区县ID
	areaIdList, err := areaService.GetAreaIdList()
	if err != nil {
		response.Fail(c)
		return
	}
	// 返回
	response.OkWithData(areaIdList, c)
}

// 通过区县ID，删除区县
func (area *AreaApi) DeleteArea(c *gin.Context) {
	// 获取区县id
	areaId := c.Param("area_id")
	global.GVA_LOG.Debug("areaId", zap.String("areaId", areaId))
	// 删除
	if err := areaService.DeleteById(areaId); err != nil {
		response.Fail(c)
		return
	}
	// 返回
	response.Ok(c)
}

// 修改区县信息
func (area *AreaApi) UpdateArea(c *gin.Context) {
	name := c.PostForm("name")
	areaId := c.PostForm("area_id")
	global.GVA_LOG.Debug("request", zap.String("name", name), zap.String("area_id", areaId))
	if name == "" || areaId == "" {
		response.FailWithMessage("参数错误", c)
		return
	}

	if err := areaService.UpdateArea(name, areaId); err != nil {
		response.Fail(c)
		return
	}
	// 返回
	response.Ok(c)
}

// 新增区县
func (area *AreaApi) AddArea(c *gin.Context) {
	// 获取县区名称
	name := c.PostForm("area_name")
	global.GVA_LOG.Debug("name", zap.String("name", name))
	if name == "" {
		response.FailWithMessage("参数错误", c)
		return
	}
	// 创建
	if err := areaService.CreateArea(name); err != nil {
		response.Fail(c)
		return
	}
	// 返回
	response.Ok(c)
}
